package com.dong.pointviewpager.transformer

import android.view.View
import androidx.viewpager.widget.ViewPager.PageTransformer
import kotlin.math.abs

/**
 * Created by Dong on 2018/3/19.
 */
class GalleryTransformer : PageTransformer {

    var pageAlpha = 0f
    var pageScale = 0f
    var pageDistance = 0f
    var pageRotation = 0f

    constructor(pageAlpha: Float, pageScale: Float, pageCenterScale: Float, pageRotation: Float) {
        this.pageAlpha = pageAlpha
        this.pageScale = pageScale
        pageDistance = pageCenterScale
        this.pageRotation = pageRotation
    }

    override fun transformPage(view: View, position: Float) {
        view.alpha = pageAlpha
        if (position > 1 && position < -1) {
            view.scaleX = pageScale
            view.scaleY = pageScale
            view.alpha = pageAlpha
            view.translationX = 0f
            view.rotationY = 0f
        } else {
            val scaleFactor = pageScale.coerceAtLeast(1 - abs(position))
            view.scaleX = scaleFactor
            view.scaleY = scaleFactor
            val alpha = 1 - abs(position)
            view.alpha = pageAlpha.coerceAtLeast(alpha)
            view.translationX = -pageDistance * position
            view.rotationY = -pageRotation * position
        }
    }
}